package com.cl.cloud.auth.service;

import com.cl.cloud.auth.model.CustomOauth2User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @description: 通过账户查询用户信息 具体实现可以自定义（可从数据库查询）
 * @author: liuzijian
 * @time: 2022-04-29 15:52
 */
@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 返回OAuth2Authentication中的Principal  （UserDetails = Principal）
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String password = passwordEncoder.encode("123456");
        return new CustomOauth2User("jihu", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
